package org.example.servicefaculty.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.model.manage.bean.po.security.User;
import org.example.model.manage.bean.po.security.UserRole;
import org.example.servicefaculty.mapper.UserMapper;
import org.example.servicefaculty.mapper.UserRoleMapper;
import org.example.servicefaculty.service.UserService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户服务实现类
 * </p>
 *
 * @author author
 * @since 2025-08-14
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    private final UserRoleMapper userRoleMapper;

    public UserServiceImpl(UserRoleMapper userRoleMapper) {
        this.userRoleMapper = userRoleMapper;
    }

    @Override
    public User createUser(User user) {
        save(user);
        Integer roleId = null;
        if ("office".equalsIgnoreCase(user.getType())) {
            roleId = 1;
        } else if ("faculty".equalsIgnoreCase(user.getType())) {
            roleId = 2;
        } else if ("counselor".equalsIgnoreCase(user.getType())) {
            roleId = 3;
        }
        if (roleId != null && user.getId() != null) {
            UserRole userRole = new UserRole();
            userRole.setUserId(user.getId());
            userRole.setRoleId(roleId);
            userRoleMapper.insertUserRole(userRole);
        }
        return user;
    }

    @Override
    public User getByAccount(String account) {
        return lambdaQuery().eq(User::getAccount, account).one();
    }

    @Override
    public User getByTypeAndTypeId(String type, Integer typeId) {
        return lambdaQuery()
                .eq(User::getType, type)
                .eq(User::getTypeId, typeId)
                .one();
    }
}